# Plugins in Rosen

## Description
Rosen has simple and ugly plugin system. You can only add any items in main menu.

## Getting started
For creating new plugin - you need to make `.py` file with `Plugin` child class inside. For example, `al_random.py`:

```py
import requests
import sys

### Rosen import all plugins in her system.
### You can use import any files from rosen project.
### For example: 

from plugins.core.plugin import Plugin
from plugins.core.pluginvideo import PluginVideo
from plugins.core.pluginplaylist import PluginPlaylist

sys.path.append("..")
from src.item import Item
from src.history import History

### You can set any name of class, what you want.

class AnilibriaRandom(Plugin):

    def __init__(self, history: History=History()):
        ##### Attributes of plugin class:
        # name              - name of plugin
        # is_query_required - is plugin work with user input?
        # query_description - description in user input window
        # history           - history object from rosen
        # 
        # flag              - cli flag in rosen
        # full_flag         - full cli flag
        # flag_help         - help text for flag
        # flag_action       - action for flag (store_true - without query, "" - with)
        # 
        ##### Attributes for plugin (not necessary)
        # api_instance      - api instance url
        # instance          - anilibria instance url
        #####

        self.name = "AnilibriaTV: Random anime"
        self.is_query_required = False
        self.query_description = """"""
        self.history = history

        self.flag = "-alr"
        self.full_flag = "--anilibriarandom"
        self.flag_help = "show random anime"
        self.flag_action = "store_true"

        self.api_instance = "https://api.anilibria.tv/v2"
        self.instance = "https://anilibria.tv"

    ### Method, who return list of items

    def get_random(self):
        ### Request to anilibria api

        resp = requests.get(f"{self.api_instance}/getRandomTitle")

        ### Return empty list if catch error
        ### Else: setup resp to his json

        if resp.status_code != 200: return list()
        resp = resp.json()

        ### Setup anime title from json, player instance and lists

        items = []
        title = f"{resp['names']['ru']} ({resp['names']['en']})"
        host = "https://" + resp["player"]["host"]
        purl = f"{self.instance}/release/{resp['code']}.html"
        videos = list()

        ### Iterate all episodes from json player playlist

        for vid in resp["player"]["playlist"].values():
            ### Setup episode num and video class
            ### with ep-num, anime name, episode url and his length

            ititle = "Episode " + str(vid["serie"])
            video = PluginVideo(
                title=ititle,
                playlist_title=title,
                url=host+vid["hls"]["hd"],
                length=resp["type"]["length"]
            )
            
            ### Check is video in history
            ### Then convert video to item class
            ### Setup video time

            inHistory = video.in_history(self.history)
            item = video.to_item(inHistory=inHistory)
            item.fstr = f"{video.length} minutes"

            ### Insert item to videos list

            videos.append(item)

        ### Setup playlist with all episodes

        playlist = PluginPlaylist(title, videos, url=purl)

        ### Convert playlist to item class

        item = playlist.to_item()

        ### Insert item to items list

        items.append(item)

        ### Return items 

        return items

    ### If is_query_required == true: call this method with user input

    def get_items_by_query(self, query: str=""):
        return list()

    ### If is_query_required == false: call this method
    
    def get_items(self):
        return self.get_random()
```

### Include plugin
For plugin including - you just need to move your plugin file to `$HOME/.config/rosen/plugins/` directory.

## Official plugins
You also can install official plugins with `plugin` command in rosen cmd.
Just launch command: `plugin {name}`.

Plugin names you can found [here](https://notabug.org/loliconshik3/rosen-plugins).
If you want - you can send pr with your plugin and after check, i add it to official repository.


